# coding=utf8
from app.extensions import db
from app.modules.common.dbUtil import createId
from app.modules.common.utils import JsonEncoder
from sqlalchemy import func


class ProjectInfo(db.Model, JsonEncoder):
    __tablename__ = "project"
    project_name = db.Column(db.String, primary_key = True)
    plan_id = db.Column(db.String)
    trunk_name = db.Column(db.String)
    local_path = db.Column(db.String)  # 工程本地路径
    vcs_path = db.Column(db.String)  # 工程svn地址
    svn_from = db.Column(db.String)  # 工程创建时的svn来源
    branch_num = db.Column(db.String)  # 分支的版本号  类似1.1.0
    project_status = db.Column(db.String)  # 工程状态
    to_test_edition = db.Column(db.String)  # 提测版本
    testing_edition = db.Column(db.String)
    to_integration_edition = db.Column(db.String)
    integration_edition = db.Column(db.String)
    integrationing_edition = db.Column(db.String)
    hand_over_edition = db.Column(db.String)
    online_edition = db.Column(db.String)
    isOnBeetle = db.Column(db.String)
    error_desc = db.Column(db.String)
    from_version = db.Column(db.String)
    from_svn_version = db.Column(db.String)
    package_type = db.Column(db.String)
    package_name = db.Column(db.String)
    svn_list = None
    plan_name = None

    def __init__(self, project_name = None, trunk_name = None, plan_id = None, local_path = None, vcs_path = None,
                 svn_from = None, branch_num = None, project_status = None, to_test_edition = None,
                 testing_edition = None, to_integration_edition = None, integration_edition = None,
                 integrationing_edition = None, hand_over_edition = None, online_edition = None, isOnBeetle = '0',
                 error_desc = None, from_version = None, package_type = None, package_name = None,
                 from_svn_version = None):
        self.project_name = project_name
        self.trunk_name = trunk_name
        self.plan_id = plan_id
        self.local_path = local_path
        self.vcs_path = vcs_path
        self.svn_from = svn_from
        self.branch_num = branch_num
        self.project_status = project_status
        self.to_test_edition = to_test_edition
        self.testing_edition = testing_edition
        self.to_integration_edition = to_integration_edition
        self.integration_edition = integration_edition
        self.integrationing_edition = integrationing_edition
        self.hand_over_edition = hand_over_edition
        self.online_edition = online_edition
        self.isOnBeetle = isOnBeetle
        self.error_desc = error_desc
        self.from_version = from_version
        self.from_svn_version = from_svn_version
        self.package_type = package_type
        self.package_name = package_name if package_name is not None else trunk_name

    def to_dict(self):
        return {
            'project_name': self.project_name,
            'trunk_name': self.trunk_name,
            'plan_id': self.plan_id,
            'plan_name': self.plan_name,
            'local_path': self.local_path,
            'vcs_path': self.vcs_path,
            'svn_from': self.svn_from,
            'branch_num': self.branch_num,
            'project_status': self.project_status,
            'to_test_edition': self.to_test_edition,
            'testing_edition': self.testing_edition,
            'to_integration_edition': self.to_integration_edition,
            'integration_edition': self.integration_edition,
            'integrationing_edition': self.integrationing_edition,
            'hand_over_edition': self.hand_over_edition,
            'online_edition': self.online_edition,
            'svn_list': self.svn_list,
            'isOnBeetle': self.isOnBeetle,
            'error_desc': self.error_desc,
            'from_version': self.from_version,
            'from_svn_version': self.from_svn_version,
            'package_type': self.package_type,
            'package_name': self.package_name,
        }


class ProjectConfig(db.Model, JsonEncoder):
    __tablename__ = "project_config"
    """
    工程配置文件修改信息
    """
    integration_id = db.Column(db.String, primary_key = True)
    integration_name = None
    trunk_name = db.Column(db.String, primary_key = True)
    key = db.Column(db.String, primary_key = True)
    value = db.Column(db.String)
    desc = db.Column(db.String)
    type = db.Column(db.String)
    is_ops_check = db.Column(db.Integer)
    file_name = db.Column(db.String)
    config_type = db.Column(db.String)
    value_from = db.Column(db.String)

    def __init__(self, integration_id = None, trunk_name = None, key = None, value = None, desc = None, type = None,
                 is_ops_check = 1, file_name = None, config_type = '其他', value_from = '参考代码'):
        self.integration_id = integration_id
        self.trunk_name = trunk_name
        self.key = key
        self.value = value
        self.desc = desc
        self.type = type
        self.is_ops_check = is_ops_check
        self.file_name = file_name
        self.config_type = config_type
        self.value_from = value_from

    def to_dict(self):
        return {
            'integration_id': self.integration_id,
            'trunk_name': self.trunk_name,
            'key': self.key,
            'value': self.value,
            'desc': self.desc,
            'type': self.type,
            'is_ops_check': self.is_ops_check,
            'file_name': self.file_name,
            'config_type': self.config_type,
            'value_from': self.value_from,
        }


class ProjectSql(db.Model, JsonEncoder):
    __tablename__ = "project_sql"
    id = db.Column(db.String, primary_key = True)
    project_name = db.Column(db.String)
    trunk_name = db.Column(db.String)
    integration_id = db.Column(db.String)
    integration_name = None
    file_name = db.Column(db.String)
    file_path = db.Column(db.String)
    file_size = db.Column(db.String)
    is_dba_check = db.Column(db.Integer)
    is_upload = db.Column(db.Integer)
    draw_time = db.Column(db.DateTime)
    modify_date = db.Column(db.String)
    file_modify_date = db.Column(db.String)
    md5 = db.Column(db.String)
    analyse = None

    def __init__(self, project_name = None, integration_id = None, trunk_name = None, file_name = None,
                 file_path = None, file_size = None, is_dba_check = 1, is_upload = "1", draw_time = None,
                 modify_date = None, file_modify_date = None, md5 = None):
        self.id = createId("ps")
        self.project_name = project_name
        self.integration_id = integration_id
        self.trunk_name = trunk_name
        self.file_name = file_name
        self.file_path = file_path
        self.file_size = file_size
        self.is_dba_check = is_dba_check
        self.is_upload = is_upload
        self.draw_time = draw_time
        self.modify_date = modify_date
        self.file_modify_date = file_modify_date
        self.md5 = md5

    def to_dict(self):
        return {
            'id': self.id,
            'project_name': self.project_name,
            'integration_id': self.integration_id,
            'integration_name': self.integration_name,
            'trunk_name': self.trunk_name,
            'file_name': self.file_name,
            'file_path': self.file_path,
            'file_size': self.file_size,
            'is_dba_check': self.is_dba_check,
            'is_upload': self.is_upload,
            'modify_date': self.modify_date,
            'file_modify_date': self.file_modify_date,
            'md5': self.md5,
            'draw_time': self.draw_time.strftime('%Y-%m-%d %H:%M:%S') if self.draw_time else None,
        }


class ProjectSqlRecord(db.Model, JsonEncoder):
    __tablename__ = "project_sql_record"
    id = db.Column(db.String, primary_key = True)
    project_name = db.Column(db.String)
    trunk_name = db.Column(db.String)
    integration_id = db.Column(db.String)
    integration_name = None
    draw_time = db.Column(db.DateTime)
    status = db.Column(db.String)
    change_desc = db.Column(db.String)

    def __init__(self, project_name = None, integration_id = None, trunk_name = None, status = None, change_desc = None,
                 draw_time = None):
        self.id = createId("psr")
        self.project_name = project_name
        self.integration_id = integration_id
        self.trunk_name = trunk_name
        self.status = status
        self.change_desc = change_desc
        self.draw_time = draw_time

    def to_dict(self):
        return {
            'id': self.id,
            'project_name': self.project_name,
            'integration_id': self.integration_id,
            'integration_name': self.integration_name,
            'trunk_name': self.trunk_name,
            'status': self.status,
            'change_desc': self.change_desc,
            'is_dba_check': self.is_dba_check,
            'draw_time': self.draw_time.strftime('%Y-%m-%d %H:%M:%S') if self.draw_time else None,
        }


class ProjectDeployTime(db.Model, JsonEncoder):
    __tablename__ = "project_deploy_time"
    trunk_name = db.Column(db.String, primary_key = True)
    zone_code = db.Column(db.String, primary_key = True)
    deploy_start_time = db.Column(db.String)
    deploy_end_time = db.Column(db.String)
    hot_deploy = db.Column(db.String)  # 是否支持非停机发版
    deployer_account = db.Column(db.String)
    deployer_realname = db.Column(db.String)
    integration_id = db.Column(db.String, primary_key = True)

    has_config = None
    has_sql = None

    def __init__(self, trunk_name = None, zone_code = None, deploy_start_time = None, deploy_end_time = None,
                 hot_deploy = None, has_config = None, has_sql = None, deployer_account = None,
                 deployer_realname = None, integration_id = None):
        self.trunk_name = trunk_name
        self.zone_code = zone_code
        self.deploy_start_time = deploy_start_time
        self.deploy_end_time = deploy_end_time
        self.hot_deploy = hot_deploy
        self.has_config = has_config
        self.has_sql = has_sql
        self.deployer_account = deployer_account
        self.deployer_realname = deployer_realname
        self.integration_id = integration_id

    def to_dict(self):
        return {
            'trunk_name': self.trunk_name,
            'zone_code': self.zone_code,
            'deploy_start_time': self.deploy_start_time,
            'deploy_end_time': self.deploy_end_time,
            'hot_deploy': self.hot_deploy,
            'has_config': self.has_config,
            'has_sql': self.has_sql,
            'deployer_account': self.deployer_account,
            'deployer_realname': self.deployer_realname,
            'integration_id': self.integration_id,
        }


class ProjectSvn(db.Model, JsonEncoder):
    __tablename__ = "project_svn"
    project_name = db.Column(db.String, primary_key = True)
    base_svn_url = db.Column(db.String)
    trunk_dir_name = db.Column(db.String)
    branches_dir_name = db.Column(db.String)

    def __init__(self, project_name = None, base_svn_url = None, trunk_dir_name = None, branches_dir_name = None):
        self.project_name = project_name
        self.base_svn_url = base_svn_url
        self.trunk_dir_name = trunk_dir_name
        self.branches_dir_name = branches_dir_name

    def to_dict(self):
        return {
            'project_name': self.project_name,
            'base_svn_url': self.base_svn_url,
            'trunk_dir_name': self.trunk_dir_name,
            'branches_dir_name': self.branches_dir_name,
        }


class ProjectOutBeetle(db.Model, JsonEncoder):
    __tablename__ = "project_out_beetle"
    trunk_name = db.Column(db.String, primary_key = True)
    package_type = db.Column(db.String)

    def __init__(self, trunk_name = None, package_type = None):
        self.trunk_name = trunk_name
        self.package_type = package_type

    def to_dict(self):
        return {
            'trunk_name': self.trunk_name,
            'package_type': self.package_type,
        }


class ProjectHandOverRecord(db.Model, JsonEncoder):
    __tablename__ = "project_hand_over_record"
    id = db.Column(db.String, primary_key = True)
    project_name = db.Column(db.String)
    edition_num = db.Column(db.String)
    create_date = db.Column(db.DateTime, default = func.now())
    hand_id = db.Column(db.String)
    hand_realname = db.Column(db.String)
    config_desc = db.Column(db.String)
    sql_desc = db.Column(db.String)
    other_desc = db.Column(db.String)
    type = db.Column(db.String)
    is_can_integration = db.Column(db.String)
    is_integration_test = db.Column(db.String)
    is_function_test = db.Column(db.String)
    is_cancel = db.Column(db.String)
    is_handle_bug = db.Column(db.String)

    def __init__(self, project_name = None, hand_id = None, hand_realname = None, config_desc = None,
                 sql_desc = None, other_desc = None, type = "TEST", is_can_integration = None,
                 is_integration_test = "1", is_function_test = "1", edition_num = None, is_cancel = '1',
                 is_handle_bug = '1'):
        self.id = createId("phor")
        self.project_name = project_name
        self.edition_num = edition_num
        self.hand_id = hand_id
        self.hand_realname = hand_realname
        self.config_desc = config_desc
        self.sql_desc = sql_desc
        self.other_desc = other_desc
        self.type = type
        self.is_can_integration = is_can_integration
        self.is_integration_test = is_integration_test
        self.is_function_test = is_function_test
        self.is_cancel = is_cancel
        self.is_handle_bug = is_handle_bug

    def to_dict(self):
        return {
            'id': self.id,
            'project_name': self.project_name,
            'edition_num': self.edition_num,
            'create_date': self.create_date.strftime('%Y-%m-%d %H:%M:%S') if self.create_date else None,
            'hand_id': self.hand_id,
            'hand_realname': self.hand_realname,
            'config_desc': self.config_desc,
            'sql_desc': self.sql_desc,
            'other_desc': self.other_desc,
            'type': self.type,
            'is_can_integration': self.is_can_integration,
            'is_integration_test': self.is_integration_test,
            'is_function_test': self.is_function_test,
            'is_cancel': self.is_cancel,
            'is_handle_bug': self.is_handle_bug,
        }


class ProjectMergeRecord(db.Model, JsonEncoder):
    __tablename__ = "project_merge_record"
    project_name = db.Column(db.String, primary_key = True)
    integration_id = db.Column(db.String)
    status = db.Column(db.String)
    desc = db.Column(db.String)
    update_date = db.Column(db.DateTime)

    def __init__(self, project_name = None, integration_id = None, status = None, desc = None, update_date = None):
        self.project_name = project_name
        self.integration_id = integration_id
        self.status = status
        self.desc = desc
        self.update_date = update_date

    def to_dict(self):
        return {
            'project_name': self.project_name,
            'integration_id': self.integration_id,
            'update_date': self.update_date.strftime('%Y-%m-%d %H:%M:%S') if self.update_date else None,
            'status': self.status,
            'desc': self.desc,
        }
